home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagn_r.zip / OOP.SWG / 0016_SORTCOLL.PAS.pas < prev    next >
Pascal/Delphi Source File  |  1993-05-28  |  4KB  |  117 lines

  1. {
  2. This post is just to demonstrate a very simple sorted collection using
  3. non-Object Types With the collection.  If it is needed to store itself
  4. to a stream, it will need additional over-ridden methods to do that.
  5. I'm just posting this, because I wrote it several days ago to implement
  6. a simple Variable system in a script language For a menu Program that I
  7. wrote, and I was looking For an *easier* way to maintain the Variable
  8. list than With a linked list.  To my astonishment, today, I needed a
  9. similar structure, and (ohmygosh) I found that I could *re-use* this
  10. code, by merely deriving a child class and adding another method or so.
  11. This is the first time that I have ever *re-used* an Object Type that I
  12. have modified.  Of course, I haven't been actually using TurboVision for
  13. more than a month or so, so I haven't had much of a chance, but it is
  14. very nice to see that when people talk about "Object oriented
  15. Programming paradigm", they are not ONLY speaking in big Words, but that
  16. they also (apparently) are telling the truth.
  17.  
  18. I'm not taking any responsibility if this overWrites your interrupt
  19. vector table, so be carefull.  If you find any mistakes, or actually
  20. modify this code to become more usefull, I'd appreciate it if you could
  21. tell me- actually determining the best way to implement a new Object
  22. class is kind of difficult For me since I've only been doing this for
  23. about a month, trying to squeeze it in along With school and a job.
  24.  
  25. Here's the code...
  26. {********* STARTS HERE **********}
  27. { Unit: STROBJ.PAS
  28.   WRITTEN BY: Brian Pape
  29.   DATE: 03/28/93
  30.   Copyright 1993 by Brian Pape and Alphawave Technologies
  31.   This Unit contains String Type Objects
  32. }
  33. {$P+}  { Enable open String parameters.  Replace by $V- For TP 6.0 or lower }
  34. Unit strobj;
  35.  
  36. Interface
  37.  
  38. Uses
  39.   Objects;
  40.  
  41. Type
  42.   str20 = String[20];
  43.  
  44.   PVarType = ^TVarType;
  45.   TVarType = Record
  46.     name  : str20;
  47.     value : String;
  48.   end;  { TVarType }
  49.  
  50.   PVarCollection = ^TVarCollection;
  51.   TVarCollection = Object(TSortedCollection)
  52.     Constructor init(Alimit,Adelta:Integer);
  53.     Function KeyOf(item:Pointer):Pointer; virtual;
  54.     Function Compare(Key1,Key2:Pointer):Integer; virtual;
  55.     Procedure freeitem(Item:Pointer); virtual;
  56.  
  57.     { This Function will return the value of a Variable in a TVarCollection }
  58.     Function getVar(s:String):String;
  59.  
  60.     { Adds a PVarType Record to the collection, without having to manually
  61.       create, and allocate memory for, a Record Type }
  62.     Procedure add(aname:str20;avalue:String);
  63.   end;  { TVarCollection }
  64.  
  65. Implementation
  66.  
  67. Constructor TVarCollection.init(ALimit,ADelta:Integer);
  68. begin
  69.   inherited init(ALimit,ADelta);
  70. end;  { TVarCollection.init }
  71.  
  72. Function TVarCollection.KeyOf(item:Pointer):Pointer;
  73. begin
  74.   KeyOf := @(TVarType(item^).name);
  75. end;  { TVarCollection.KeyOf }
  76.  
  77. Function TVarCollection.Compare(Key1,Key2:Pointer):Integer;
  78. begin
  79.   if String(Key1^) > String(Key2^) then
  80.     Compare := 1
  81.   else if String(Key1^) = String(Key2^) then
  82.     Compare := 0
  83.   else Compare := -1;
  84. end;  { TVarCollection.Compare }
  85.  
  86. Procedure TVarCollection.freeitem(Item:Pointer);
  87. begin
  88.   dispose(Item);
  89. end;  { freeitem }
  90.  
  91. Function TVarCollection.getVar(s:String):String;
  92. Var
  93.   t : TVarType;
  94.   where : Integer;
  95. begin
  96.   t.name := s;
  97.   if Search(@t,where) then
  98.     getVar := TVarType(at(where)^).value
  99.   else
  100.     getVar := '';
  101. end;  { getVar }
  102.  
  103.  
  104. Procedure TVarCollection.add(aname:str20;avalue:String);
  105. Var
  106.   rec : PVarType;
  107. begin
  108.   rec := new(PVarType);
  109.   rec^.name := aname;
  110.   rec^.value := avalue;
  111.   insert(rec);
  112. end;  { add }
  113.  
  114. begin
  115. end.  { strobj }
  116. {*********** endS HERE *************}
  117.